﻿// (c) Copyright Fernando Cerqueira.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System.IO;
using System.Windows.Markup;
using System.Windows.Media.Animation;

// ReSharper disable CheckNamespace
namespace DevPhone.Controls
// ReSharper restore CheckNamespace
{
    public class ConvertXamlFile
    {
        private static ConvertXamlFile _instance;
        private static readonly object Lockinstance = new object();

        public static ConvertXamlFile Instance()
        {
            lock (Lockinstance)
            {
                if (_instance == null)
                {
                    _instance = new ConvertXamlFile();
                }
            }
            return _instance;
        }

        private ConvertXamlFile()
        {
        }

        public T Read<T>(string fileName)
        {
            return Read<T>("DevPhone.Controls.Storyboards", fileName);
        }

        public T Read<T>(string path, string fileName)
        {
            string result = null;

            using (var stream = GetType().Assembly.GetManifestResourceStream(path + "." + fileName))
            {
                if (stream != null)
                {
                    var sr = new StreamReader(stream);
                    result = sr.ReadToEnd();
                }
            }

            if (result != null)
            {
                return (T)XamlReader.Load(result);
            }
            return default(T);
        }

    }
}
